/*
 * Created by SharpDevelop.
 * User: rseward
 * Date: 7/4/2008
 * Time: 10:27 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using AvvaMobileNHibernateHelperKit.Xml;
using AvvaMobileNHibernateHelperKit.Utils;

namespace AvvaMobileNHibernateHelperKit.DAO
{
	/// <summary>
	/// Description of Class1.
	/// </summary>
	public class ProjectDao
	{
		private static ProjectDao instance = new ProjectDao();
		public static ProjectDao Instance {
			get { return instance; }
			set { instance = value; }			
		}
		
		private ProjectDao()
		{
		}
		
		public Project GetProject(string filename) 
		{
			Project project = null;
			FileInfo projFile = new FileInfo(filename);
			
			if (projFile.Exists) {
				String xml = FileUtils.GetFileText(projFile);
				Xml.Project projXml = (Xml.Project) XmlUtils.ParseXml(new Xml.Project(), xml);
				
				project = new Project();
				project.ProjectFile = projFile.FullName;
				project.AssemblyName = projXml.AssemblyName;
				project.DatabaseName = projXml.DatabaseName;
				project.InstanceName = projXml.InstanceName;
				project.Namespace = projXml.Namespace;
				project.Password = projXml.Password;
				project.ProjectDir = projXml.ProjectDir;
				project.ServerName = projXml.ServerName;
				project.Username = projXml.Username;
				project.CopyNullablesDll = projXml.CopyNullablesDll;
				project.CopyNHibernateDll = projXml.CopyNHibernateDll;
				project.CopyLog4netDll = projXml.CopyLog4netDll;
				
				if (projXml.HbmOptions!=null) {
					project.DynamicUpdate = projXml.HbmOptions.DynamicUpdate;
				}
				
				List<String> tables = new List<String>();
				foreach(Xml.Table xmlTable in projXml.SelectedTables) {
					tables.Add( xmlTable.name );
				}
				project.SelectedTables = tables.ToArray();
				
				project.ClassLanguageGenerator = LanguageGenerator.CSharp;
				if (projXml.ClassLanguageGenerator == "vb.net") {
					project.ClassLanguageGenerator = LanguageGenerator.VBNet;
				}				
			}
			
			return project;
		}
		
		public void SaveProject(Project project)
		{
			FileInfo projFile = new FileInfo( project.ProjectFile );
			
			Xml.Project xmlProj = new Xml.Project();
			
			xmlProj.AssemblyName = project.AssemblyName;
			xmlProj.DatabaseName = project.DatabaseName;
			xmlProj.Namespace = project.Namespace;
			xmlProj.InstanceName = project.InstanceName;
			xmlProj.Password = project.Password;
			xmlProj.ProjectDir = project.ProjectDir;
			xmlProj.ServerName = project.ServerName;
			xmlProj.Username = project.Username;
			
			xmlProj.HbmOptions = new HbmOptions();
			xmlProj.HbmOptions.DynamicUpdate = project.DynamicUpdate;
			xmlProj.CopyLog4netDll = project.CopyLog4netDll;
			xmlProj.CopyNHibernateDll = project.CopyNHibernateDll;
			xmlProj.CopyNullablesDll = project.CopyNullablesDll;
			
			List<Xml.Table> selectedTables = new List<Xml.Table>();
			foreach (string table in project.SelectedTables) {
				Xml.Table xmlTable = new Xml.Table();
				xmlTable.name = table;
				selectedTables.Add( xmlTable );				
			}
			xmlProj.SelectedTables = selectedTables.ToArray();
			
			xmlProj.ClassLanguageGenerator = "csharp";
			if (project.ClassLanguageGenerator == LanguageGenerator.VBNet ) {
				xmlProj.ClassLanguageGenerator = "vb.net";
			}
			
			if (!projFile.Directory.Exists) {
				projFile.Directory.Create();
			}
			
			String xml = XmlUtils.ToXml(xmlProj);
			
			StreamWriter writer = new StreamWriter( projFile.FullName );			
			try {
				writer.Write(xml);
			} finally {
				writer.Close();
			}
		}
		

		
	}
}
